<template>
	<div>
		<p><h3>批次信息</h3></p>
		<el-divider ></el-divider>
		<div id="batchInfoDiv">
      <el-table
      :data="batchInfo"
      :header-cell-style="{background:'#eef1f6',color:'#606266','text-align':'center',fontSize:'15px'}"
      :cell-style="{'text-align':'center'}"
      style="width: 100%">
          <el-table-column
            :label="batchName"
            >
            <el-table-column
            prop="levelName"
            label="批次等级">
            </el-table-column>
            <el-table-column
            prop="phaseTeacherStart"
            label="批次申报开始日期">
            </el-table-column>
            <el-table-column
            prop="phaseTeacherEnd"
            label="批次申报结束日期">
            </el-table-column>
            <el-table-column
            prop="phaseDepartmentStart"
            label="批次学院评审开始日期">
            </el-table-column>
            <el-table-column
            prop="phaseDepartmentEnd"
            label="批次学院评审结束日期">
            </el-table-column>
            <el-table-column
            label="批次最大项目通过数">
            {{maxPassNum}}
            </el-table-column>
            <el-table-column
            prop="date"
            label="批次预通过数">
            {{batchPrePassNumber}}
            </el-table-column>
          </el-table-column>
      </el-table>
		</div>
    <div id="ProjectInfo">
        <el-dialog title="意见填写" :visible.sync="dialogTableVisible" @close="closeDialog">
                <div style="margin-bottom: 30px;">
                    意见类型:
                    <el-radio v-model="dialogProjectInfo.proApproval" label="2">通过</el-radio>
                    <el-radio v-model="dialogProjectInfo.proApproval" label="5">驳回</el-radio>
                    <el-radio v-model="dialogProjectInfo.proApproval" label="6">不通过</el-radio>
                </div>
                学院评分:
                <el-input autocomplete="off"
                :disabled="inputState"
                style="width: 200px;"
                placeholder="请输入分数"
                v-model="dialogProjectInfo.departmentScore"
                size="medium">
                </el-input>
                <div>
                  <div style="margin-top: 20px;">
                  	<quill-editor v-model="dialogProjectInfo.proCommittee"> </quill-editor>
                  </div>
                </div>
                <div slot="footer" class="dialog-footer">
                  <el-button size="medium" type="primary" @click="dialogSubmit">暂存</el-button>
                </div>
        </el-dialog>

        <p><h3>项目信息</h3></p>
        <el-divider ></el-divider>
        <el-button type="primary" @click="lotSizeRevoke">批量撤销</el-button>
        <el-button type="warning" @click="lotSizeNoppass">批量不通过</el-button>
        <el-button type="success" @click="auditSubmit">审核提交</el-button>
        <div id="ProjectTable" style="margin-top: 25px;">
          <el-table
              :stripe='true'
              ref="multipleTable"
              :data="tableData"
              tooltip-effect="dark"
              style="width: 100%"
              @selection-change="handleSelectionChange"
              :header-cell-style="{'text-align':'center',background:'#eef1f6',color:'#606266',}"
              :cell-style="{'text-align':'center'}"
              >
              <el-table-column
                type="selection"
                width="55">
              </el-table-column>
              <el-table-column
                label="项目名称"
                width="120">
                <template slot-scope="scope">{{ scope.row.proName }}</template>
              </el-table-column>
              <el-table-column
                prop="classification"
                label="科目"
                width="120">
              </el-table-column>
              <el-table-column
                prop="memberName"
                label="主持人"
                width="120">
              </el-table-column>
              <el-table-column
                label="查看详情"
                width="120">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" @click="viewDetails(scope.row,scope.$index)">查看详情</el-button>
                </template>
              </el-table-column>
              <el-table-column
                label="意见类型"
                width="120">
                <template slot-scope="scope">
                  <span v-if="scope.row.proApproval==2 || scope.row.proApproval==5||scope.row.proApproval==6">
                      <p v-if="scope.row.proApproval==2">通过</p>
                      <p v-if="scope.row.proApproval==5">驳回</p>
                      <p v-if="scope.row.proApproval==6">不通过</p>
                  </span>
                  <span v-else>未填写</span>
                </template>
              </el-table-column>
              <el-table-column
                label="学院意见"
               show-overflow-tooltip>
                <template slot-scope="scope">
                  <span v-if="scope.row.proCommittee==null || scope.row.proCommittee==''">
                    未填写
                  </span>
                  <span v-else>
                    {{getSimpleText(scope.row.proCommittee)}}
                  </span>
                </template>

              </el-table-column>
              <el-table-column
                prop="departmentScore"
                sortable
                label="学院分数"
                :formatter="scoreFormat"
                width="120">
              </el-table-column>
              <el-table-column
                label="操作"
                show-overflow-tooltip>
                <template slot-scope="scope">
                  <span v-if="scope.row.proApproval==2 || scope.row.proApproval==5||scope.row.proApproval==6">
                    <el-button size="mini" type="danger" @click="revokeSubmit(scope.$index,scope.row)" >撤销</el-button>
                  </span>
                  <span v-else>
                    <el-button size="mini" type="success" @click="opinionFilling(scope.$index,scope.row)">意见填写</el-button>
                  </span>
                </template>
              </el-table-column>
            </el-table>
        </div>

    </div>
	</div>

</template>

<script>
export default {
		data() {
			return {
        //临时index
        tempIndex:'',
        dialogTableVisible:false,
        batchName:'',
				thisBatchId:'',
        dialogProjectInfo:{
          departmentScore:'',
          committeeDate:'',
          proCommittee:'',
          proApproval:'',
        },
				batchInfo:[],
				batchPrePassNumber:0,
        teacherInfo:'',
        maxPassNum:'',
        tableData: [],
        multipleSelection: [],
        inputState:'',
			}
		},
    created() {
      this.thisBatchId=this.$route.query.batch_id;
      this.teacherInfo=this.$store.state.info
      console.log(this.teacherInfo)
      this.getBatchInfo()
      this.getThisDepBatchMaxPassNum()
      this.getProjectDataAxios()
    },
    mounted(){
    },
    computed:{
      proApproval(){
          return this.dialogProjectInfo.proApproval
      }
    },
    watch:{
      proApproval(){
        if(this.dialogProjectInfo.proApproval==2){
          this.inputState=false
        }else{
          this.inputState=true
        }
      }
    },
		methods: {
      scoreFormat(row,column){
          if(row.departmentScore==null || row.departmentScore==''){
            return '未填写'
          }else{
            return row.departmentScore
          }
      },
      auditSubmit(){
        var Surplus
        Surplus=this.maxPassNum-this.batchPrePassNumber
        for(let i=0;i<this.tableData.length;i++){
          if(this.tableData[i].proApproval=='' || this.tableData[i].proCommittee==''){
            this.$message({
              type: 'error',
              message: '尚有项目未作出选择!',
              duration: 2000,
              offset:screen.height/2-200,
              center:true
            })
            return
          }
        }
        if(this.batchPrePassNumber>this.maxPassNum){
          this.$message({
            type: 'error',
            message: '批次预通过数大于最大项目通过数!',
            duration: 2000,
            offset:screen.height/2-200,
            center:true
          })
          return
        }

        if(Surplus>0){
           const h = this.$createElement;
              this.$msgbox({
                title: '提示消息',
                message: h('p', null, [
                  h('span', null, '您还有剩余'),
                  h('i', { style: 'color: red' }, Surplus),
                  h('span', null, ' 名额，是否继续提交？'),
                ]),
                showCancelButton: true,
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                beforeClose: (action, instance, done) => {
                  if (action === 'confirm') {
                    instance.confirmButtonLoading = true;
                    instance.confirmButtonText = '执行中...';
                    setTimeout(() => {
                      done();
                      setTimeout(() => {
                        instance.confirmButtonLoading = false;
                      }, 300);
                    }, 3000);
                  } else {
                    done();
                  }
                }
              }).then(action => {
                this.$message({
                  type: 'success',
                  message: '审核提交成功!',
                  duration: 2000,
                  offset:screen.height/2-200,
                  center:true
                })
                this.UpdateBatchInfo()
              });
        }else{
            this.$message({
              type: 'success',
              message: '审核提交成功!',
              duration: 2000,
              offset:screen.height/2-200,
              center:true
            })
            this.$store.commit('getTodoList')
            this.UpdateBatchInfo()
        }
      },
      //更新批次状态
      UpdateBatchInfo(){
        this.$api.batch.updateBatchStatus({
          batchId:this.thisBatchId,
          status:1
        }).then((response)=>{
          console.log(response)
          this.goAuditBatch()
        })
      },
      goAuditBatch(){
        this.$router.push({path:'/depApprovalAudit'})
      },
      closeDialog(){
        Object.keys(this.dialogProjectInfo).forEach(key=>{this.dialogProjectInfo[key]=''})
        this.dialogTableVisible=false
      },
      viewDetails(row,index){
        this.$router.push({path:'/depProjectAuditPdf',query:{proId:row.proId,batchId:this.thisBatchId}})
      },
      getNowDate(){
        var nowDate = new Date();
        var year = nowDate.getFullYear();
        var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1): nowDate.getMonth() + 1;
        var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
         .getDate();
        var dateStr = year + "-" + month + "-" + day;
        return  dateStr
      },
      getSimpleText(html){
        var re1 = new RegExp("<.+?>","g");//匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
        var msg = html.replace(re1,'');//执行替换成空字符
        return msg;
      },
      //提交操作
      dialogSubmit(){
        var numReg = /^[0-9]*$/
        var numRe = new RegExp(numReg)

        if(this.dialogProjectInfo.proApproval==''){
          this.$message({
            type: 'warning',
            message: '请选择意见类型!',
            duration: 1000,
            offset:screen.height/2-200,
            center:true
          })
          return false
        }

        if(this.dialogProjectInfo.proApproval==2){
          if(this.dialogProjectInfo.departmentScore==''){
            this.$message({
              type: 'warning',
              message: '学院评分不能为空!',
              duration: 1000,
              offset:screen.height/2-200,
              center:true
            })
            return false
          }
          //分数不能重复

       for(let i=0 ; i<this.tableData.length;i++){
         if(this.tableData[i].departmentScore==this.dialogProjectInfo.departmentScore){
           this.$message({
             type: 'warning',
             message: '学院评分不能重复!',
             duration: 1000,
             offset:screen.height/2-200,
             center:true
           })
           return false
         }
       }
          if (!numRe.test(this.dialogProjectInfo.departmentScore) || this.dialogProjectInfo.departmentScore>100 || this.dialogProjectInfo.departmentScore<0) {
            this.$message({
              type: 'warning',
              message: '请输入0-100的数字 ',
              duration: 1000,
              offset:screen.height/2-200,
              center:true
            })
            return false
          }
        }else{
          this.dialogProjectInfo.departmentScore='0'
        }

        if(this.dialogProjectInfo.proCommittee==''){
          this.$message({
            type: 'warning',
            message: '请填写学院意见!',
            duration: 1000,
            offset:screen.height/2-200,
            center:true
          })
          return false
        }

        //赋值
        var projectMembers,projectSearchings,projectTeachings,project
        var today=this.getNowDate()
        this.tableData[this.tempIndex].departmentScore=this.dialogProjectInfo.departmentScore
        this.tableData[this.tempIndex].committeeDate=this.dialogProjectInfo.committeeDate
        this.tableData[this.tempIndex].proCommittee=this.dialogProjectInfo.proCommittee
        this.tableData[this.tempIndex].proApproval=this.dialogProjectInfo.proApproval
        Object.keys(this.dialogProjectInfo).forEach(key=>{this.dialogProjectInfo[key]=''})
        this.$api.project.getProjectById({
          proId:this.tableData[this.tempIndex].proId
        }).then((response)=>{
          console.log(response)
          //做项目更新操作的临时数据
          project=response.data.data.project
          projectMembers=response.data.data.members
          projectSearchings=response.data.data.searchings
          projectTeachings=response.data.data.teachings

          this.$api.project.updateProject({
            projectMembers:JSON.stringify(projectMembers) ,
            projectSearchings:JSON.stringify(projectSearchings),
            projectTeachings:JSON.stringify(projectTeachings),
            proId:this.tableData[this.tempIndex].proId,
            batchId:this.thisBatchId,
            committeeDate:today,
            proApproval: this.tableData[this.tempIndex].proApproval,
            departmentScore:this.tableData[this.tempIndex].departmentScore,
            proCommittee:this.tableData[this.tempIndex].proCommittee
          })
          .then((response)=>{
            this.getProjectDataAxios()
            console.log(response)
          })
          //创建 记录
          this.$api.project.creatRecord({
              gh:this.teacherInfo.gh,
              name:this.teacherInfo.xm,
              proId:project.proId,
              state:this.tableData[this.tempIndex].proApproval,
              time:this.getNowFormatDate(),
              message:this.getSimpleText(this.tableData[this.tempIndex].proCommittee)
          }).then((response)=>{
            console.log(response)
          })
          this.$message({
          	message: "意见填写成功!",
          	type: "success",
          	offset: screen.height / 2 - 100,
          	center: true,
            duration:1000
          })
        })
        this.$store.commit("decreaseTodoListNumber")
        if(this.tableData[this.tempIndex].proApproval==2){
          this.batchPrePassNumber=this.batchPrePassNumber+1
        }

        this.dialogTableVisible=false
      },
      getNowFormatDate() {
          var date = new Date();
          var seperator1 = "-";
          var seperator2 = ":";
          var month = date.getMonth() + 1;
          var strDate = date.getDate();
          if (month >= 1 && month <= 9) {
              month = "0" + month;
          }
          if (strDate >= 0 && strDate <= 9) {
              strDate = "0" + strDate;
          }
          var Hours= date.getHours()
          if (Hours >= 0 && Hours <= 9) {
              Hours = "0" + Hours;
          }

          var Minutes= date.getMinutes()
          if (Minutes >= 0 && Minutes <= 9) {
              Minutes = "0" + Minutes;
          }

          var Seconds= date.getSeconds()
          if (Seconds >= 0 && Seconds <= 9) {
              Seconds = "0" + Seconds;
          }
          var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                  + " " + Hours + seperator2 + Minutes
                  + seperator2 + Seconds;
          return currentdate;
      },
      //撤回操作
      revokeSubmit(index,row){
        if(row.proApproval==2){
          this.batchPrePassNumber-=1
        }
        //赋值
        this.tempIndex=index
        var projectMembers,projectSearchings,projectTeachings,project
        var today=this.getNowDate()
        this.tableData[this.tempIndex].departmentScore=this.dialogProjectInfo.departmentScore
        this.tableData[this.tempIndex].committeeDate=this.dialogProjectInfo.committeeDate
        this.tableData[this.tempIndex].proCommittee=this.dialogProjectInfo.proCommittee
        this.tableData[this.tempIndex].proApproval=this.dialogProjectInfo.proApproval
        console.log( this.tableData[this.tempIndex])
        Object.keys(this.dialogProjectInfo).forEach(key=>{this.dialogProjectInfo[key]=''})
        this.$api.project.getProjectById({
          proId:this.tableData[this.tempIndex].proId
        }).then((response)=>{
          //做项目更新操作的临时数据
          project=response.data.data.project
          projectMembers=response.data.data.members
          projectSearchings=response.data.data.searchings
          projectTeachings=response.data.data.teachings

          this.$api.project.updateProject({
            projectMembers:JSON.stringify(projectMembers) ,
            projectSearchings:JSON.stringify(projectSearchings),
            projectTeachings:JSON.stringify(projectTeachings),
            proId:this.tableData[this.tempIndex].proId,
            batchId:this.thisBatchId,
            committeeDate:'',
            proApproval: -1,
            departmentScore:'',
            proCommittee:''
          })
          .then((response)=>{
            console.log(response)
          })
          //创建 记录
          this.$api.project.creatRecord({
              gh:this.teacherInfo.gh,
              name:this.teacherInfo.xm,
              proId:project.proId,
              state:-1,
              time:this.getNowFormatDate(),
              message:'撤销操作'
          }).then((response)=>{
            console.log(response)
          })

        })
        this.$message({
          	message: "撤销操作成功!",
          	type: "success",
          	offset: screen.height / 2 - 100,
          	center: true,
            duration:1000
          })
      },
      //批量撤回
      lotSizeRevoke(){
        console.log(this.multipleSelection)
        var projectMembers,projectSearchings,projectTeachings,project
        for(let i=0;i<this.multipleSelection.length;i++){
            if(this.multipleSelection[i].proApproval!=2 && this.multipleSelection[i].proApproval!=5 && this.multipleSelection[i].proApproval!=6 ){
              this.$message({
              	message: "请勿选择不服符合该的操作项目!",
              	type: "warning",
              	offset: screen.height / 2 - 100,
              	center: true,
                duration:1000
              })
              return
              break
            }}
        for(let i=0;i<this.multipleSelection.length;i++){
            if(this.tableData[i]==2){
              this.batchPrePassNumber-=1
            }
            this.$api.project.getProjectById({
              proId:this.multipleSelection[i].proId
            }).then((response)=>{
              console.log(response)
              //做项目更新操作的临时数据
              project=response.data.data.project
              projectMembers=response.data.data.members
              projectSearchings=response.data.data.searchings
              projectTeachings=response.data.data.teachings

              this.$api.project.updateProject({
                projectMembers:JSON.stringify(projectMembers) ,
                projectSearchings:JSON.stringify(projectSearchings),
                projectTeachings:JSON.stringify(projectTeachings),
                proId:project.proId,
                batchId:this.thisBatchId,
                committeeDate:'',
                proApproval:-1,
                departmentScore:'',
                proCommittee:''
              })
              .then((response)=>{
                console.log(response)
                this.$message({
                	message: "批量撤销操作成功!",
                	type: "success",
                	offset: screen.height / 2 - 100,
                	center: true,
                  duration:1000
                })
                this.getProjectDataAxios()
              })
              //创建 记录
              this.$api.project.creatRecord({
                  gh:this.teacherInfo.gh,
                  name:this.teacherInfo.xm,
                  proId:project.proId,
                  state:-1,
                  time:this.getNowFormatDate(),
                  message:'撤销操作'
              }).then((response)=>{
                console.log(response)
              })
            })
        }
        this.$refs.multipleTable.clearSelection();
      },
      //批量不通过
      lotSizeNoppass(){
        console.log(this.multipleSelection)
        var projectMembers,projectSearchings,projectTeachings,project
        for(let i=0;i<this.multipleSelection.length;i++){
            if(this.multipleSelection[i].proApproval==6){
              this.$message({
              	message: "请勿选择已操作项目!",
              	type: "warning",
              	offset: screen.height / 2 - 100,
              	center: true,
                duration:1000
              })
              return
            }
            this.$api.project.getProjectById({
              proId:this.multipleSelection[i].proId
            }).then((response)=>{
              console.log(response)
              //做项目更新操作的临时数据
              project=response.data.data.project
              projectMembers=response.data.data.members
              projectSearchings=response.data.data.searchings
              projectTeachings=response.data.data.teachings

              this.$api.project.updateProject({
                projectMembers:JSON.stringify(projectMembers) ,
                projectSearchings:JSON.stringify(projectSearchings),
                projectTeachings:JSON.stringify(projectTeachings),
                proId:project.proId,
                batchId:this.thisBatchId,
                committeeDate:this.getNowDate(),
                proApproval:6,
                departmentScore:0,
                proCommittee:'不通过'
              })
              .then((response)=>{
                console.log(response)
                this.getProjectDataAxios()

                this.$message({
                	message: "批量不通过操作成功!",
                	type: "success",
                	offset: screen.height / 2 - 100,
                	center: true,
                  duration:1000
                })
              })
              //创建 记录
              this.$api.project.creatRecord({
                  gh:this.teacherInfo.gh,
                  name:this.teacherInfo.xm,
                  proId:project.proId,
                  state:6,
                  time:this.getNowFormatDate(),
                  message:'不通过'
              }).then((response)=>{
                console.log(response)
              })
            })
        }
        this.$refs.multipleTable.clearSelection();
      },
      opinionFilling(index,row){
        this.tempIndex=index
        this.dialogTableVisible=true
      },
			getBatchInfo(){
				this.$api.batch.getAllBatch()
				.then((response)=>{
					console.log(response.data.data)
					this.batchInfo.push(response.data.data.find(item=>item.batchId==this.thisBatchId))
          console.log(this.batchInfo)
          this.batchName=this.batchInfo[0].batchName
				})
			},
      getThisDepBatchMaxPassNum(){
        this.$api.project.getAllNumProject({batchId:this.thisBatchId}).then((response)=>{
            this.maxPassNum=response.data.data.find(item => item.depId==this.teacherInfo.bmbh).maxPronum
        })
      },
        getProjectDataAxios() {
          let that = this;
          this.$api.batch.getProjectByBatchId({
        	batchId: this.thisBatchId
          }).then(
            (response)=>{
        	  console.log(response)
              this.tableData=response.data.data.filter((item)=>{
               return item.proDepId==this.teacherInfo.bmbh && item.ranking==1 && item.proApproval==1 ||
                      item.proDepId==this.teacherInfo.bmbh && item.ranking==1 && item.proApproval==2 ||
                      item.proDepId==this.teacherInfo.bmbh && item.ranking==1 && item.proApproval==6 ||
                      item.proDepId==this.teacherInfo.bmbh && item.ranking==1 && item.proApproval==5
              })
              this.batchPrePassNumber=0
              for(let i=0;i<this.tableData.length;i++){
                  if(this.tableData[i].proApproval==2){
                    this.batchPrePassNumber=this.batchPrePassNumber+1
                  }
              }
        	})
        },
       toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
		},
	}

</script>

<style scoped>
	#batchInfoDivContent{
		margin: 20px;
	}
	#batchInfoDivContent div{
		margin-top: 20px;
	}
	div > p,h4{
		display: inline;
	}


</style>
